﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using System.Data.SQLite;
namespace FF.Method
{
    public class UserMethod
    {
        
        #region 添加
        public static int userAdd(User user)
        {
            int intFalg = 0;
            string sql = "insert into ff_user(name,type,password,remark) values( ";
           // sql += "'"+user.UserId+"',";
            sql += "'" + user.UserName + "',";
            sql += "'" + user.Type + "',";
            sql += "'" + user.Password + "',";
            sql += "'" + user.Remark + "'";
            //sql += "'" + user.isAdmin + "'";
            sql += ")";
            intFalg = SQLiteHelper.ExecuteSql(sql);
            return intFalg;
        }
        #endregion
        #region 修改
        public static int userUpdate(User user)
        {

            string sql = "update ff_user set ";
            //sql += "user_id='"+user.UserId;
            sql += "name='" + user.UserName;
            sql += "',password='" + user.Password;
            sql += "',remark='" + user.Remark;
            sql += "',type='" + user.Type;
            sql += "' where user_id='" + user.UserId + "'";
            return SQLiteHelper.ExecuteSql(sql);
        }
        #endregion
        #region 删除
        public static int userDelete(string userId)
        {
            string sql = "delete from ff_user where user_id='" + userId + "'";
            return SQLiteHelper.ExecuteSql(sql);
        }
        #endregion
        #region 查询
        /// <summary>
        /// 执行查询语句，返回单个User
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>User</returns>
        public static User getUserById(string userId)
        {
            User user = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select * from ff_user where user_id='" + userId + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    user = new User();
                    user.UserId = reader.GetInt32(0);
                    user.UserName = reader.GetString(1);
                    user.Type = reader.GetString(2);
                    user.Password = reader.GetString(3);
                    user.Remark = reader.GetString(4);
                    //user.isAdmin = reader.GetString(5);
                }
                reader.Close();
                return user;
            }catch(Exception ex)
            {
                MessageBox.Show("程序出错了！"+ex.Message,"提示信息 ",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return null;
            }

        }
        public static User getUserByPasswordAndName(string name, string password)
        {
             User user = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select * from ff_user where name='" + name + "' and password='" + password + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    user = new User();
                    user.UserId = reader.GetInt32(0);
                    user.UserName = reader.GetString(1);
                    user.Type = reader.GetString(2);
                    user.Password = reader.GetString(3);
                    user.Remark = reader.GetString(4);
                    //user.isAdmin = reader.GetString(5);
                }
                reader.Close();
                return user;
            }catch(Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }
        /// <summary>
        /// 根据用户类型名称得到用户类型
        /// </summary>
        /// <param name="userTypeName"></param>
        /// <returns>string</returns>
        public static string getUserTypeByName(string userTypeName)
        {
            string userType = null;
            SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select * ff_user where name=" + userTypeName);
            if(reader.HasRows)
            {
                reader.Read();
                userType = reader.GetString(0);
            }
            reader.Close();
            return userType;

        }
        #endregion

        #region //生成用户户编号 例如：KH-20071118114255
        public static string generateUserID()
        {
            int intYear = DateTime.Now.Day;
            int intMonth = DateTime.Now.Month;
            int intDate = DateTime.Now.Year;
            int intHour = DateTime.Now.Hour;
            int intSecond = DateTime.Now.Second;
            int intMinute = DateTime.Now.Minute;
            string strTime = null;
            strTime = intYear.ToString();
            if (intMonth < 10)
            {
                strTime += "0" + intMonth.ToString();
            }
            else
            {
                strTime += intMonth.ToString();
            }
            if (intDate < 10)
            {
                strTime += "0" + intDate.ToString();
            }
            else
            {
                strTime += intDate.ToString();
            }
            if (intHour < 10)
            {
                strTime += "0" + intHour.ToString();
            }
            else
            {
                strTime += intHour.ToString();
            }
            if (intMinute < 10)
            {

                strTime += "0" + intMinute.ToString();
            }
            else
            {
                strTime += intMinute.ToString();
            }
            if (intSecond < 10)
            {

                strTime += "0" + intSecond.ToString();
            }
            else
            {
                strTime += intSecond.ToString();
            }


            return ("YH-" + strTime);



        }// end if 
        #endregion

        public static void fillUser(object objTreeView, object obimage)
        {
            if (objTreeView.GetType().ToString() == "System.Windows.Forms.TreeView")
            {
                System.Windows.Forms.ImageList imlist = (System.Windows.Forms.ImageList)obimage;
                System.Windows.Forms.TreeView TV = (System.Windows.Forms.TreeView)objTreeView;
                TV.Nodes.Clear();
                TV.ImageList = imlist;
                try
                {
                    SQLiteDataReader readerType = SQLiteHelper.ExecuteReader("select * from ff_user_type" );
                    SQLiteDataReader rederUser;
                    if (readerType.HasRows)
                    {
                        System.Windows.Forms.TreeNode TN;
                        while (readerType.Read())
                        {
                            TN = TV.Nodes.Add(readerType.GetString(0), readerType.GetString(1), 1, 0);
                            rederUser = SQLiteHelper.ExecuteReader("select user_id, name from ff_user where type='" + readerType.GetString(0) + "'");
                            if(rederUser.HasRows)
                            {
                                while(rederUser.Read())
                                {
                                    //TreeNode newNode12 = new TreeNode(rederUser.GetString(0), 1, 0);
                                    //newNode12.Nodes.Add("A", qlddr[4].ToString(), 0, 1);
                                    //TN.Nodes.Add(newNode12);
                                    TN.Nodes.Add(rederUser.GetInt32(0).ToString(), rederUser.GetString(1), 1, 0);
                                }
                            }
                            rederUser.Close();

                            //System.Windows.Forms.TreeNode TN = TV.Nodes.Add("B", "商品B", 1, 0);

                            // System.Windows.Forms.TreeNode TN = TV.Nodes.Add("C", "商品C", 1, 0);

                            //TreeNode newNode12 = new TreeNode("草果ID", 1, 0);
                            //newNode12.Nodes.Add("A", "苹果", 0, 1);
                            //TN.Nodes.Add(newNode12);

                            
                        }
                        TV.ExpandAll();
                        
                    }
                    readerType.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
               
            }
        }


    }
}